package com.hxwb.chainmaker.rpc.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hxwb.chainmaker.rpc.common.MessageResult;
import com.hxwb.chainmaker.rpc.param.AddHashCodeRes;
import com.hxwb.chainmaker.rpc.param.BalanceOfHttpRes;
import com.hxwb.chainmaker.rpc.param.GetHashCodeRes;
import com.hxwb.chainmaker.rpc.param.TransferHttpRes;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

public class HttpUtils {
    public static BalanceOfHttpRes balanceOf(String address,String symbol) {
        try{
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put("address",address);
            paramMap.put("symbol",symbol);
            System.out.println(paramMap);
            String result = cn.hutool.http.HttpUtil.get(PathUtil.balanceOfUrl,paramMap);
            System.out.println(result);
            JSONObject jsonObject = JSON.parseObject(result);
            BalanceOfHttpRes httpRes = JSON.toJavaObject(jsonObject, BalanceOfHttpRes.class);
            return httpRes;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }

    }
    public static GetHashCodeRes getHashCodeList(String address,String symbol) {
        try{
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put("address",address);
            paramMap.put("symbol",symbol);
            System.out.println(paramMap);
            String result = cn.hutool.http.HttpUtil.get(PathUtil.getHashCodeList,paramMap);
            System.out.println(result);
            JSONObject jsonObject = JSON.parseObject(result);
            GetHashCodeRes httpRes = JSON.toJavaObject(jsonObject, GetHashCodeRes.class);
            return httpRes;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }
    }
    public static TransferHttpRes transfer(String fromAddress, String toAddress, String privateKey, String symbol, BigDecimal amount) {
        try{
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put("fromAddress",fromAddress);
            paramMap.put("toAddress",toAddress);
            paramMap.put("privateKey",privateKey);
            paramMap.put("amount",amount);
            paramMap.put("symbol",symbol);
            System.out.println(paramMap);
            String request = JSON.toJSONString(paramMap);
            String result = HttpRequest.post(PathUtil.transferUrl)
                    .body(request)//表单内容
                    .timeout(20000)//超时，毫秒
                    .execute().body();
            System.out.println(result);;
            JSONObject jsonObject = JSON.parseObject(result);
            TransferHttpRes httpRes = JSON.toJavaObject(jsonObject, TransferHttpRes.class);
            return httpRes;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }

    }

    public static AddHashCodeRes addHashCode(String address, String privateKey, String symbol, String  hashCode) {
        try{
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put("address",address);
            paramMap.put("privateKey",privateKey);
            paramMap.put("symbol",symbol);
            paramMap.put("hashCode",hashCode);
            System.out.println(paramMap);
            String request = JSON.toJSONString(paramMap);
            String result = HttpRequest.post(PathUtil.addHashCode)
                    .body(request)//表单内容
                    .timeout(20000)//超时，毫秒
                    .execute().body();
            System.out.println(result);;
            JSONObject jsonObject = JSON.parseObject(result);
            AddHashCodeRes httpRes = JSON.toJavaObject(jsonObject, AddHashCodeRes.class);
            return httpRes;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }

    }
    public static MessageResult transferchain(Long fromUserId,Long toUserId, BigDecimal amount, String symbol) {
        try {
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put("toUserId", toUserId);
            paramMap.put("fromUserId", fromUserId);
            paramMap.put("amount", amount);
            paramMap.put("symbol", symbol);
            System.out.println(paramMap);
            String request = JSON.toJSONString(paramMap);
            String result = HttpRequest.post(PathUtil.chainmaker)
                    .body(request)//表单内容
                    .timeout(20000)//超时，毫秒
                    .execute().body();
            System.out.println(result);
            JSONObject jsonObject = JSON.parseObject(result);
            MessageResult httpRes = JSON.toJavaObject(jsonObject, MessageResult.class);
            return httpRes;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }

    }
}
